<template>
  <div class="login-container">
    <div class="login-form-wrap">
       <!-- 头部 -->
       <div class="login-heade">
         <div class="logo"></div>
       </div>
       <!-- 表单部分 -->
       <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
            <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
            <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="agree">
            <el-checkbox v-model="form.agree" label="我已阅读并同意用户协议和隐私条款" name="type"></el-checkbox>
        </el-form-item>
        <el-form-item>
            <el-button :loading="loginLoading" class="login-btn" type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
       </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 加载
      loginLoading: false,
      form: {
        mobile: '13911111111',
        code: '246810',
        type: [],
        agree: true // 是否同意协议
      },
      rules: {
        mobile: [
          { required: true, message: '请输入正确的手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确!!!', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入正确的密码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '验证码格式不正确!!!' }
        ],
        agree: [
          // 自定义校验规则
          // { required: true, message: '请同意协议', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              // console.log(rule, value)
              if (value) {
                // 选中，验证通过
                callback()
              } else {
                // 如果没选中，给出错误提示
                callback(new Error('请同意用户协议！！！'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      // 1.获取表单input的内容
      console.log(this.form.mobile, this.form.code)

      // 2.进行表单校验
      this.$refs.form.validate(res => {
        // 加载状态改变
        this.loading = true
        console.log('校验结果', res) // true / false
        if (res) {
          this.login()
        }
      })
    },
    async login () {
      // async await另一种异步方法解决回调地狱
      try {
        // 3.发送axios请求，调用接口
        const result = await this.$http({
          method: 'POST',
          url: '/mp/v1_0/authorizations',
          data: {
            mobile: this.form.mobile,
            code: this.form.code
          }
        })
        // 4.登录成功提示/失败提示
        this.$message.success('登录成功')
        // 加载状态改变
        this.loginLoading = true
        // 成功登录后，
        //   1.提取token
        const token = result.data.data.token
        // console.log(token)
        //   2.将token保存到localStorage中
        localStorage.setItem('tokenStr', token)
        //   3.保存token后跳转到layout界面
        this.$router.push('/layout')
        // eslint-disable-next-line handle-callback-err
      } catch (error) {
        this.$message.error('登陆失败了')
        this.loginLoading = true
      }
    }
  }
}
</script>

<style scoped lang="less">
// 铺满整屏
.login-container{
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  // 居中显示
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url('../../assets/login_bg.jpg') no-repeat;
  background-size: cover;
  .login-form-wrap{
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-heade{
      // 居中显示
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url('../../assets/logo_index.png') no-repeat;
        background-size: contain;
      }
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
